package com.pt25.base.dep.cassandra;


import com.pt25.base.exception.BizException;
import com.pt25.base.util.StrUtil;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Data
@Component
@ConfigurationProperties(prefix = "cassandra-configs")
class CassandraProperties {

    /**
     * 默认集成名称
     */
    private String primary;

    private List<CassandraConfig> configs;

    @Data
    static class CassandraConfig {
        /**
         * es集群名称
         */
        private String name;
        /**
         * 集群地址
         */
        private String contactPoints;
        /**
         * 端口
         */
        private Integer port;
        /**
         * 账号
         */
        private String username;
        /**
         * 密码
         */
        private String password;
        /**
         * 连接信息配制
         */
        private Integer localNewMaxRequests = 16;
        private Integer localNewCoreConnections = 2;
        private Integer localNewMaxConnections = 4;
        private Integer remoteNewCoreConnections = 2;
        private Integer remoteNewMaxConnections = 4;
    }

    Boolean checkConfigs() {
        Set<String> nameSet = new HashSet<>();
        configs.forEach(config -> {
            nameSet.add(config.getName());

            if (StrUtil.isBlank(config.getName())) {
                throw new BizException("name is empty");
            }
            if (StrUtil.isBlank(config.getContactPoints())) {
                throw new BizException("contactPoints is empty");
            }
            if (StrUtil.isBlank(config.getUsername())) {
                throw new BizException("username is empty");
            }
            if (StrUtil.isBlank(config.getPassword())) {
                throw new BizException("password is empty");
            }
            if (null == config.getPort() || config.getPort() <= 0) {
                throw new BizException("port is empty or less than 0");
            }
        });
        if (nameSet.size() != configs.size()) {
            throw new BizException("cassandra集群名称有重复！");
        }
        return true;
    }
}
